$(function() {
	// 年月级联
	var yearAndMonth = function() {
		var year = $('select[name="year"]');
		var month = $('select[name="month"]');

		if (!year.val()) {
			var options = $('option', month);
			options.removeAttr('selected');
			options.eq(0).attr('selected', 'selected');
			month.attr('disabled', 'disabled');
		} else {
			month.removeAttr('disabled');
		}
	};

	// 缓存图表
	var charts = {};

	// 柱状图
	var barChart = function(elem, xData, yData, dataName, labelFormat) {
		var chart = echarts.init(elem, 'infographic');

		chart.showLoading({
			text : '正在努力的读取数据中...'
		});

		var option = {
			tooltip : {
				trigger : 'axis'
			},
			legend : {
				data : [ dataName ]
			},
			toolbox : {
				show : true,
				feature : {
					saveAsImage : {
						show : true
					}
				}
			},
			calculable : true,
			xAxis : [ {
				type : 'category',
				axisLabel : {
					'interval' : 0,
					rotate : 0
				},
				data : xData
			} ],
			yAxis : [ {
				type : 'value'
			} ],
			series : [ {
				name : dataName,
				type : 'bar',
				itemStyle : {
					normal : {
						label : {
							show : true,
							position : 'top',
							formatter : labelFormat
						}
					}
				},
				data : yData
			} ]
		};

		chart.setOption(option);

		chart.hideLoading();

		return chart;
	};

	// 饼图
	var pieChart = function(elem, legend, data, dataName, title, formatter) {
		var chart = echarts.init(elem, 'macarons');

		chart.showLoading({
			text : '正在努力的读取数据中...'
		});

		var option = {
			title : {
				text : title,
				x : 'center'
			},
			tooltip : {
				trigger : 'item',
				formatter : formatter
			},
			legend : {
				orient : 'vertical',
				x : 'left',
				data : legend
			},
			toolbox : {
				show : true,
				feature : {
					saveAsImage : {
						show : true
					}
				}
			},
			calculable : true,
			series : [ {
				name : dataName,
				type : 'pie',
				radius : '70%',
				center : [ '50%', '60%' ],
				data : data
			} ]
		};

		chart.setOption(option);

		chart.hideLoading();

		return chart;
	};

	// 画图表
	var drawChart = function() {
		var form = $('form.report-form');

		form.ajaxSubmit({
			success : function(data, textStatus, jqXHR) {
				if (!data)
					data = {};

				var selector = 'div.report.money';
				var chart = $(selector);
				if (charts[selector] && charts[selector].dispose) {
					charts[selector].dispose();
					charts[selector] = false;
				}
				if (data.money) {
					var xData = data.money.xData, yData = data.money.yData;

					var money = barChart(chart.get(0), xData, yData, '金额', '￥{c}');

					charts[selector] = money;
				} else {
					chart.html('暂无数据');
				}

				selector = 'div.report.status';
				chart = $(selector);
				if (charts[selector] && charts[selector].dispose) {
					charts[selector].dispose();
					charts[selector] = false;
				}
				if (data.status) {
					var xData = data.status.xData, yData = data.status.yData;

					var status = barChart(chart.get(0), xData, yData, '数量', '{c}');

					charts[selector] = status;
				} else {
					chart.html('暂无数据');
				}

				selector = 'div.report.manager';
				chart = $(selector);
				if (charts[selector] && charts[selector].dispose) {
					charts[selector].dispose();
					charts[selector] = false;
				}
				if (data.manager) {
					var legend = data.manager.legend, d = data.manager.data;

					var manager = pieChart(chart.get(0), legend, d, '数量', '项目经理管理项目数量占比', "{b}<br>{a}：{c}<br>占比：{d}%");

					charts[selector] = manager;
				} else {
					chart.html('暂无数据');
				}

				selector = 'div.report.customer';
				chart = $(selector);
				if (charts[selector] && charts[selector].dispose) {
					charts[selector].dispose();
					charts[selector] = false;
				}
				if (data.customer) {
					var legend = data.customer.legend, d = data.customer.data;

					var customer = pieChart(chart.get(0), legend, d, '数量', '客户项目数量占比', "{b}<br>{a}：{c}<br>占比：{d}%");

					charts[selector] = customer;
				} else {
					chart.html('暂无数据');
				}
			},
			error : function(jqXHR, textStatus, errorThrown) {
				alert('获取数据失败！\n' + errorThrown);
			}
		});
	};

	// 表单提交
	$('form.report-form select').on('change', function() {
		var $this = $(this);

		// 年月级联
		if ($this.attr('name') == 'year') {
			yearAndMonth();
		}

		drawChart();
	});

	// 刷新图表数据
	$('.report.panel .refresh-report').on('click', function() {
		drawChart();
		return false;
	});

	// 初始化图表
	drawChart();
});